package com.cqs.learning.spring.cloud.producer.web;

import com.cqs.learning.spring.cloud.producer.service.TestOtService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lixw
 * @date 2020/3/27 7:55
 */
@RestController
@Log4j2
public class TestPerformanceController {

    @Autowired
    private TestOtService service;

    private final static ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000));

    //传统的
    @RequestMapping("/m1")
    public String hello() {
        int i = service.cnt();
//        log.info(Thread.currentThread().getName() + "同步处理结束");
        return String.format("普通的Mvc-%d",i);
    }

    //异步
    @RequestMapping("/m20")
    public DeferredResult<String> executeSlowTask20() {
        DeferredResult<String> deferredResult = new DeferredResult<>();
        //使用JDK自带的线程
        CompletableFuture.supplyAsync(service::cnt).whenCompleteAsync((result, throwable) -> {
            if (throwable != null) {
                log.error("放生异常了", throwable);
                throw new RuntimeException(throwable);
            }

//            log.info("返回结果:" + deferredResult.getResult());
        });
        deferredResult.setResult("SUCCESS");
//        log.info(Thread.currentThread().getName() + "异步处理结束");
        return deferredResult;
    }

    //异步
    @RequestMapping("/m2")
    public DeferredResult<String> executeSlowTask() {
        DeferredResult<String> deferredResult = new DeferredResult<>();
        //使用JDK自带的线程
        CompletableFuture.runAsync(service::cnt).whenCompleteAsync((result, throwable) -> {
            if (throwable != null) {
                log.error("放生异常了", throwable);
                throw new RuntimeException(throwable);
            }
            deferredResult.setResult(String.valueOf(result));
//            log.info("返回结果:" + deferredResult.getResult());
        });
        log.info(Thread.currentThread().getName() + "异步处理结束");
        return deferredResult;
    }

    @RequestMapping("/m3")
    public DeferredResult<String> executeSlowTask3() {
        DeferredResult<String> deferredResult = new DeferredResult<>();
        //使用自定义线程
        CompletableFuture.supplyAsync(service::cnt, pool).whenCompleteAsync((result, throwable) -> {
            if (throwable != null) {
                log.error("放生异常了", throwable);
                throw new RuntimeException(throwable);
            }
            deferredResult.setResult(String.valueOf(result));
        });
        log.info(Thread.currentThread().getName() + "异步处理结束");
        return deferredResult;
    }

    @RequestMapping("/m4")
    public DeferredResult<String> executeSlowTask4() {
        System.out.println("控制层执行线程:" + Thread.currentThread().getName());
        //超时
        DeferredResult<String> deferredResult = new DeferredResult<String>(10 * 1000L);
        deferredResult.onTimeout(new Runnable() {
            @Override
            public void run() {
                System.out.println("异步线程执行超时");
                deferredResult.setResult("线程执行超时");
            }
        });
        deferredResult.onCompletion(new Runnable() {
            @Override
            public void run() {
                log.info(Thread.currentThread().getName() + "异步处理结束");
            }
        });
        pool.submit(() -> {
            int cnt = service.cnt();
            deferredResult.setResult(String.valueOf(cnt));
        });
        return deferredResult;
    }


}
